﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace Project
{
    public partial class addLoan : Form
    {
        string loanID;
        string iid;
        private OleDbConnection bookConn;
        private OleDbCommand oleDbCmd = new OleDbCommand();
        private String connParam = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Properties.Settings.Default.database + ";Persist Security Info=False";
        string currID;
        public addLoan(string id)
        {
            currID = id;
            bookConn = new OleDbConnection(connParam);
            InitializeComponent();
        }


        string iName;
        private void button1_Click(object sender, EventArgs e)
        {
            loanview.DataSource = null;
            loanview.Rows.Clear();
            loanview.Refresh();
            
            OleDbDataAdapter dAdapter = new OleDbDataAdapter("select * from item", connParam);
            OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);
            DataTable dataTable = new DataTable();
            DataSet ds = new DataSet();
            dAdapter.Fill(dataTable);

            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                loanview.Rows.Add(dataTable.Rows[i][0], dataTable.Rows[i][1], dataTable.Rows[i][2], dataTable.Rows[i][3]);
            }
        }
        
        private void button3_Click(object sender, EventArgs e)
        {
            if (lblAvailable.Text == "Yes")
            {
                DateTime time = DateTime.Now;
                string dateFormat = "dd/MM/yyyy";
                DateTime returnDate = DateTime.Now;




                if ((txtReturn.Text == "") || (Convert.ToInt32(txtReturn.Text) < 0))
                {
                    if (txtReturn.Text == "")
                        MessageBox.Show("Please enter number of days for rent OR Select date from calender.");
                    else if (Convert.ToInt32(txtReturn.Text) < 0)
                        MessageBox.Show("Selected date is before current date.");
                }
                else
                {
                    returnDate = returnDate.AddDays(Convert.ToUInt16(txtReturn.Text));
                    bookConn.Open();
                    oleDbCmd.Connection = bookConn;

                    oleDbCmd.CommandText = "insert into loan (sID,iID,start_date,end_date,status, iName) values('" + currID + "', '" + iid + "', '" + time.ToString(dateFormat) + "', '" + returnDate.ToString(dateFormat) + "', 'rented', '" + iName + "') ;";
                    try
                    {
                        int temp = oleDbCmd.ExecuteNonQuery();
                        if (temp > 0)
                        {
                            oleDbCmd.CommandText = "UPDATE loan SET hold = '' where iID = '" + iid + "' ;";
                            oleDbCmd.ExecuteNonQuery();
                            DialogResult dialogResult = MessageBox.Show("Loan added successfully.\n\nLoan another item?", "", MessageBoxButtons.YesNo);
                            if (dialogResult == DialogResult.Yes)
                            {
                                //do something
                            }
                            else if (dialogResult == DialogResult.No)
                            {
                                bookConn.Close();
                                this.Close();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Record Fail to Added");
                        }
                    }
                    catch
                    {
                        MessageBox.Show("Record Fail to Added");
                    }







                    bookConn.Close();

                }
            }
            else
            {
                DialogResult dialogResult = MessageBox.Show("Item is not available for rent.\n\nWould you like to hold it for ID: " + currID, "", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    bookConn.Open();
                    oleDbCmd.Connection = bookConn; 
                    oleDbCmd.CommandText = "update loan SET hold = '" + currID + "' WHERE ID =  "+loanID+" ;";
                    try
                    {
                        int temp = oleDbCmd.ExecuteNonQuery();
                        if (temp > 0)
                        {

                            MessageBox.Show("Record Successfuly Added");
                        }
                        else
                        {
                            MessageBox.Show("Record Fail to Added");
                        }
                    }
                    catch
                    {
                        MessageBox.Show("Record Fail to Added");
                    }
                    bookConn.Close();
                }
                else if (dialogResult == DialogResult.No)
                {
                    //do something
                }
            }
        }
        

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void itemDetail(string ID)
        {




            OleDbDataAdapter dAdapter = new OleDbDataAdapter("select * from loan where (iID = '" + ID + "' AND status = 'rented')", connParam);
            OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);
            DataTable dataTable = new DataTable();
            DataSet ds = new DataSet();
            dAdapter.Fill(dataTable);

            int ifFound = dataTable.Rows.Count;
            if (ifFound > 0)
            {
                lblAvailable.Text = "No";
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    loanID = dataTable.Rows[i][0].ToString();
                    lblReturn.Text = dataTable.Rows[i][4].ToString();
                    if (dataTable.Rows[i][8].ToString() != "")
                        lblHold.Text = "Yes (" + dataTable.Rows[i][8].ToString() + ")";
                    else
                        lblHold.Text = "No";
                }
            }
            else
            {
                lblAvailable.Text = "Yes";
                lblReturn.Text = "";
                OleDbDataAdapter dAdapter1 = new OleDbDataAdapter("select * from loan where (iID = '" + ID + "' AND status = 'returned')ORDER BY hold", connParam);
                OleDbCommandBuilder cBuilder1 = new OleDbCommandBuilder(dAdapter1);
                DataTable dataTable1 = new DataTable();
                DataSet ds1 = new DataSet();
                dAdapter1.Fill(dataTable1);
                
                for (int i = 0; i < dataTable1.Rows.Count; i++)
                {
                                     
                    if (dataTable1.Rows[i][8].ToString() != "")
                        lblHold.Text = "Yes (" + dataTable1.Rows[i][8].ToString() + ")";
                    else
                        lblHold.Text = "No";
                }


            }
        }
        
        
        
        
        private void loanview_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (loanview.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
                {
                    lblAvailable.Text = "";
                    iid = loanview.Rows[e.RowIndex].Cells[0].Value.ToString();
                   iName = loanview.Rows[e.RowIndex].Cells[1].Value.ToString();
                    itemDetail( iid);          
                }
            }
            catch
            {
                //
            }
        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            DateTime time = DateTime.Now;
            // Use current time

            DateTime returnDate = DateTime.Now;
            returnDate = returnDate.AddDays(17);

            DateTime time1 = monthCalendar1.SelectionRange.Start;
            TimeSpan span = time1 - time;
            int diffDays = (time1.Date - time.Date).Days;
            txtReturn.Text = diffDays.ToString();
        }

       
    }
}
